//
//  MySpriteButton.cpp
//  CanYouDoIt
//
//  Created by hoangtx on 7/6/13.
//
//

#include "MySpriteButton.h"
#include "MyUtilities.h"

bool MySpriteButton::init(){
    normalColor = ccc3(60,200,30);
    highlightColor = ccc3(200,32,40);
    return true;
}
MySpriteButton* MySpriteButton::create(const char *fileName, double mX, double mY, double mWidth, double mHeight){

    MySpriteButton* button = MySpriteButton::create();
    button->setPosition(ccp(mX,mY));
    button->buttonSize = CCSizeMake(mWidth, mHeight);
    button->sprite = CCSprite::create(fileName);
    button->sprite->setAnchorPoint(ccp(0,0));
    button->sprite->setScaleX(mWidth/button->sprite->getContentSize().width);
    button->sprite->setScaleY(mHeight/button->sprite->getContentSize().height);
    button->sprite->setPosition(ccp(0,0));
    button->sprite->setColor(button->normalColor);
    button->addChild(button->sprite);
    return button;
}
void MySpriteButton::initialize(const char *fileName, double mX, double mY, double mWidth, double mHeight){
    this->setPosition(ccp(mX,mY));
    this->buttonSize = CCSizeMake(mWidth, mHeight);
    this->sprite = CCSprite::create(fileName);
    this->sprite->setAnchorPoint(ccp(0,0));
    this->sprite->setScaleX(mWidth/this->sprite->getContentSize().width);
    this->sprite->setScaleY(mHeight/this->sprite->getContentSize().height);
    this->sprite->setPosition(ccp(0,0));
    this->sprite->setColor(this->normalColor);
    this->addChild(this->sprite);
}


void MySpriteButton::setColorState(ccColor3B mNormalColor, ccColor3B mHighLightColor){
    normalColor = mNormalColor;
    highlightColor = mHighLightColor;
    sprite->setColor(normalColor);
}
//void MySpriteButton::updateState(){
//    if(isFocused){
//        this->sprite->setColor(highlightColor);
//    }else{
//        this->sprite->setColor(normalColor);
//    }
//}
void MySpriteButton::goToNormalMode(){
    this->sprite->setColor(normalColor);
}
void MySpriteButton::goToHighlightMode(){
    this->sprite->setColor(highlightColor);
}

bool MySpriteButton::isTouchedAt(cocos2d::CCPoint touchPosition){
    CCPoint originPoint = ccp( getPositionX() + buttonSize.width/2, getPositionY() + buttonSize.height/2);
    double distance = MyUtilities::distanceOfTwoPoints(originPoint, touchPosition);
    double radius = buttonSize.width/2;
    if(distance <= radius){
        return true;
    }else{
        return false;
    }
}

